package com.smartcar.client.util;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.smartcar.client.domin.Destination;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 高德地图工具类
 *
 * @author : 高文娜大美女
 * @className : MapUtil
 * @description : 描述说明该类的功能
 * @createTime : 2023/10/18 14:04
 */
@Log4j2
@Component
public class MapUtil {
    private static final String KEY = "b4b97cbbc0b6cb1979fe19453a34a1f5";

    /**
     * 根据地址  获取经纬度
     *
     * @param destination
     * @return
     */
    public Destination show(String destination) {

        String body = HttpUtil.createGet("https://restapi.amap.com/v3/geocode/geo?address=" + destination + "&output=JSON&key=" + KEY)
                .execute().charset("utf-8").body();
        log.info(body);
        JSONObject response = JSON.parseObject(body);

        // 获取geocodes JSONArray中的第一个元素
        JSONObject geocode = response.getJSONArray("geocodes").getJSONObject(0);

        // 获取相应字段对应的值
        String formattedAddress = geocode.getString("formatted_address");
        String location = geocode.getString("location");

        // 打印输出处理结果
        log.info("格式化地址：" + formattedAddress);
        log.info("经纬度：" + location);
        String[] split = location.split(",");
        Destination dest = new Destination();
        dest.setLongitude(split[0]);
        dest.setLatitude(split[1]);
        log.info(dest);
        return dest;
    }

    /**
     * 根据经纬度获取地理位置
     */
    public Destination show3(Destination destination) {
        String body = HttpUtil.createGet("https://restapi.amap.com/v3/geocode/regeo?output=JSON&location=" + destination.getLongitude() + "," + destination.getLatitude() + "&key=" + KEY + "")
                .execute().charset("utf-8").body();
        Map map = JSON.parseObject(body, Map.class);//
        Map o1 =(Map) map.get("regeocode");
        String formattedAddress =(String) o1.get("formatted_address");
        log.info("地址："+formattedAddress);
        Destination build =
                Destination.builder().
                        address(formattedAddress)
                        .build();
        return build;
    }

    /**
     * 测试
     *
     * @param
     */
    public void show2(String origin, String destination) {
        String body =
                HttpUtil.createGet("https://restapi.amap.com/v3/geocode/regeo?output=JSON&location=116.310003,39.991957&key=" + KEY + "")
                        .execute().
                        charset("utf-8")
                        .body();
        log.info(body);
    }

    /**
     * 测试
     * 根据经纬度获取地理位置
     *
     * @param
     */
    public static void main() {
        String body = HttpUtil.createGet("https://restapi.amap.com/v3/geocode/regeo?output=JSON&location=116.310003,39.991957&key=" + KEY + "")
                .execute().charset("utf-8").body();
        log.info(body);

    }

    public static void main(String[] args) {
        String body = HttpUtil.createGet("https://restapi.amap.com/v3/geocode/geo?address=山西省运城市稷山县&output=JSON&key=" + KEY)
                .execute().charset("utf-8").body();
        log.info(body);
        JSONObject response = JSON.parseObject(body);

        // 获取geocodes JSONArray中的第一个元素
        JSONObject geocode = response.getJSONArray("geocodes").getJSONObject(0);

        // 获取相应字段对应的值
        String formattedAddress = geocode.getString("formatted_address");
        String location = geocode.getString("location");

        // 打印输出处理结果
        log.info("格式化地址：" + formattedAddress);
        log.info("经纬度：" + location);
        String[] split = location.split(",");
        Destination dest = new Destination();
        dest.setLongitude(split[0]);
        dest.setLatitude(split[1]);
        log.info(dest);
    }
}
